package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.bean.Account;
import model.bo.CheckLoginAdminBO;
import common.Privilege;

/**
 * Servlet implementation class CheckLoginAdminServlet
 */
public class CheckLoginAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckLoginAdminServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userName = request.getParameter("userName");
	    String password = request.getParameter("password");
	    
	    CheckLoginAdminBO checkLoginAdminBO = new CheckLoginAdminBO();
	    boolean isValid = checkLoginAdminBO.isValid(userName, password);
	    
	    if (isValid) {
	        HttpSession session = request.getSession();
	        Account account = checkLoginAdminBO.getAccount(userName);
	        session.setAttribute("account", account);
	        switch (account.getPrivilege()) {
                case Privilege.ADMIN:
                	response.sendRedirect("administrator/profile.jsp");
                	break;
                case Privilege.MANAGE_TOUR:
                    response.sendRedirect("manage_tour/profile.jsp");
                    break;
                case Privilege.MANAGE_SERVICE:
                    response.sendRedirect("manage_service/profile.jsp");
                    break;
                case Privilege.MANAGE_NEWS:
                    
                    break;
                case Privilege.MANAGE_TRANSACTION:
                    
                    break;
                default:
                    
                    break;
            }
	    }else {
	        // userName or password is not correct.
	        request.setAttribute("errorMessage", "Username or Password is not correct");
	        RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
	        rd.forward(request, response);
	    }
	}

}
